 package com.weixin.servlet.information;

import java.io.IOException;
import java.util.Date;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.gson.Gson;
import com.weixin.service.INoticeService;
import com.weixin.service.impl.NoticeServiceImpl;
import com.weixin.util.PageUtil;
import com.weixin.vo.Notice;
import com.weixin.vo.Pay;


@WebServlet("/NoticeServlet/*")
public class NoticeServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
    INoticeService noticeService = new NoticeServiceImpl();
    

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		doGet(request, response);
	}

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		String pathInfo = request.getPathInfo().substring(1);
		if("listAll".equals(pathInfo)) {
			listAll(request,response);
		}else if("add".equals(pathInfo)) {
			add(request,response);
		}else if("findAllBySplit".equals(pathInfo)) {
			findAllBySplit(request,response);
		}else if("preEdit".equals(pathInfo)) {
			preEdit(request,response);
		}else if("removeById".equals(pathInfo)) {
			removeById(request,response);
		}else if("edit".equals(pathInfo)) {
			edit(request,response);
		}
	}

	/**
	 * �޸Ĺ�����Ϣ
	 * @param request
	 * @param response
	 */
	private void edit(HttpServletRequest request, HttpServletResponse response) {
		//���ñ����ʽ
		try {
			request.setCharacterEncoding("utf-8");
			//��jsp�õ�ֵ
			String notice_name = request.getParameter("notice_name");
			String notice_desc = request.getParameter("notice_desc");
			int notice_id = 0;
			if(request.getParameter("notice_id")!=null&&!(request.getParameter("notice_id").equals(""))) {
				notice_id = Integer.valueOf(request.getParameter("notice_id"));
			}
			//����ֵ
			Notice vo = new Notice();
			vo.setNotice_name(notice_name);
			vo.setNotice_desc(notice_desc);
			vo.setNotice_id(notice_id);
			vo.setNotice_time(new Date());
			vo.setAdmin_id(1001);
			//�ض�������ҳ����
			if(noticeService.edit(vo)) {
				response.sendRedirect(request.getContextPath()+"/NoticeServlet/findAllBySplit?pageNum=1");
			}else {
				
			}
			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	/**
	 * ɾ��������Ϣ
	 * @param request
	 * @param response
	 */
	private void removeById(HttpServletRequest request, HttpServletResponse response) {
		String[] notices = request.getParameterValues("notice_id");
		
		//ɾ������
		try {
			if(noticeService.removeById(notices)) {
				response.getWriter().println(1);
			}else {
				response.getWriter().println(0);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * �������ҳ����޸ĵ�����Ԥ�޸ķ���
	 * @param request
	 * @param response
	 */
	private void preEdit(HttpServletRequest request, HttpServletResponse response) {
		response.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		int notice_id = 0;
		if(request.getParameter("notice_id")!=null&&!(request.getParameter("notice_id").equals(""))) {
			notice_id = Integer.valueOf(request.getParameter("notice_id"));
		}
		try {
			//���ݹ���id�ҵ�������Ϣ
			Notice notice = noticeService.findById(notice_id);
			if(notice!=null) {
				response.getWriter().println("["+new Gson().toJson(notice)+"]");
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	/**
	 * �������û����
	 * @param request
	 * @param response
	 */
	private void listAll(HttpServletRequest request, HttpServletResponse response) {
		// TODO Auto-generated method stub
		
	}

	/**
	 * notice���ҳ��ѯ
	 * @param request
	 * @param response
	 */
	private void findAllBySplit(HttpServletRequest request, HttpServletResponse response) {
		int pageNum = Integer.valueOf(request.getParameter("pageNum"));
		try {
			//��÷�ҳ����
			List<Notice> noticeList = noticeService.findAllBySplit(pageNum, PageUtil.PAGESIZE);
			//���������
			int total = noticeService.findTotal();
			//������ҳ��
			int pageCount = (int) Math.ceil(total*1.0/PageUtil.PAGESIZE);
			//request��������Գ�ԱList
			request.setAttribute("noticeList", noticeList);
			request.setAttribute("pageCount", pageCount);
			request.setAttribute("pageNum", pageNum);
			//ת��������ҳ��
			request.getRequestDispatcher("/pages/notice/noticeManage_list.jsp").forward(request, response);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	/**
	 *�������ģ�����ӷ���
	 * @param request
	 * @param response
	 */
	private void add(HttpServletRequest request, HttpServletResponse response) {
		try {
			//���ñ����ʽ
			request.setCharacterEncoding("utf-8");
			//��jsp�õ�������ֵ
			String notice_desc = request.getParameter("notice_desc");
			String notice_name = request.getParameter("notice_name");
			Notice vo = new Notice();
			//����
			vo.setNotice_name(notice_name);
			vo.setNotice_desc(notice_desc);
			vo.setNotice_time(new Date());
			vo.setAdmin_id(1001);
			//�����Ƿ����гɹ��������������Ϣ
			boolean b = noticeService.add(vo);
			if(b) {
				request.setAttribute("message","��ӳɹ�");
			}else{
				request.setAttribute("message", "���ʧ��");
			}
			//�ض�����findAllBySplit����
			response.sendRedirect(request.getContextPath()+"/NoticeServlet/findAllBySplit?pageNum=1");
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}


	}

